#include<iostream>
#include<cctype>
#include<cstring>
#include<fstream>
using namespace std;

void handle_1() {
    cout << "Enter, type @ to termiate input:" << endl;
    char ch;
    cin.get(ch);
    while (ch != '@') {
        if (islower(ch)) ch = toupper(ch);
        else if (isupper(ch)) ch = tolower(ch);
        if (!isdigit(ch)) cout << ch;
        cin.get(ch);
    }
    cout << endl;
}

void handle_2() {
    double donation[10];
    int count = 0;
    double sum = 0;
    cout << "Enter 10 donation:" << endl;
    int i = 0;
    for (; i < 10; i++) {
        cin >> donation[i];
        if (cin.fail()) {
            cout << "Not a digit!" << endl;
            break;
        }
        sum += donation[i];
    }
    double average = sum / i;
    for (i = 0; i < 10; i++) {
        if (donation[i] > average) {
            count++;
        }
    }
    cout << "The average is " << average << endl;
    cout << count << " values are greater than the average" << endl;
}

void handle_3() {
    cout << "Please enter one of the following choices:" << endl;
    cout << "c) carnivore\t" << "p) pianist" << endl;
    cout << "t) tree\t\t" << "g) game" << endl;
    char ch;
    cin.get(ch);
    while (isalpha(ch)) {
        switch (ch)
        {
        case 'c': cout << "A maple is a carnivore." << endl;
            break;
        case 'p': cout << "A maple is a pianist." << endl;
            break;
        case 't': cout << "A maple is a tree." << endl;
            break;
        case 'g': cout << "A maple is a game." << endl;
            break;
        default: cout << "Please enter a c, p, t, or g: ";
            cin.ignore();
        }
        cin.get(ch);
    }
}

const int strsize = 20;

struct bop {
    char fullname[strsize];
    char title[strsize];
    char bopname[strsize];
    int preference;
};

void handle_4() {
    cout << "Benevolent Order of Programmers Report" << endl;
    cout << "a. display by name\t\t" << "b. display by title" << endl;
    cout << "c. display by bopname\t\t" << "d. display by preference" << endl;
    cout << "q. quit" << endl;
    cout << "Enter your choice: ";
    bop* list = new bop[5];
    strcpy(list[0].fullname, "Wimp Macho");
    strcpy(list[1].fullname, "Raki Rhodes");
    strcpy(list[2].fullname, "Celia Laiter");
    strcpy(list[3].fullname, "Hoppy Hipman");
    strcpy(list[4].fullname, "Pat Hand");
    strcpy(list[0].bopname, "bop0");
    strcpy(list[1].bopname, "bop1");
    strcpy(list[2].bopname, "bop2");
    strcpy(list[3].bopname, "bop3");
    strcpy(list[4].bopname, "bop4");
    strcpy(list[0].title, "title0");
    strcpy(list[1].title, "title1");
    strcpy(list[2].title, "title2");
    strcpy(list[3].title, "title3");
    strcpy(list[4].title, "title4");
    list[0].preference = 0;
    list[1].preference = 0;
    list[2].preference = 0;
    list[3].preference = 0;
    list[4].preference = 0;
    char ch;
    cin >> ch;
    while (ch != 'q') {
        switch (ch)
        {
        case 'a': 
            for (int i = 0; i < 5; i++) {
                cout << list[i].fullname << endl;
            }
            break;
        case 'b':
            for (int i = 0; i < 5; i++) {
                cout << list[i].title << endl;
            }
            break;
        case 'c':
            for (int i = 0; i < 5; i++) {
                cout << list[i].bopname << endl;
            }
            break;
        case 'd':
            for (int i = 0; i < 5; i++) {
                int tip = list[i].preference;
                switch (tip)
                {
                case 0: cout << list[i].fullname << endl;
                    break;
                case 1: cout << list[i].title << endl;
                    break;
                case 2: cout << list[i].bopname << endl;
                    break;
                default: cout << "illegal!" << endl;
                    break;
                }
            }
            break;
        default: cout << "illegal!" << endl;
            cin.ignore();
            break;
        }
        cout << "Enter your choice: ";
        cin >> ch;
    }
    cout << "Bye!" << endl;
}

void handle_5() {
    double income, tax;
    cout << "Enter your income: ";
    while(cin >> income) {
        if (income < 0) {
            cout << "Illegal Input" << endl;
            break;
        }
        if (income <= 5000) {
            tax = 0;
        } else if (income <= 15000) {
            tax = (income - 5000) * 0.1;
        } else if (income <= 35000) {
            tax = 10000 * 0.1 + (income - 15000) * 0.15;
        } else {
            tax = 10000 * 0.1 + 20000 * 0.15 + (income - 35000) * 0.2;
        }
        cout << "Your tax is " << tax << endl;
        cout << "Enter your income: ";
    }
    if (cin.fail()) cout << "Illegal Input" << endl;
}

struct StoreMess {
    string name;
    double money;
};

void handle_6() {
    int num;
    cout << "Enter the number of donors: ";
    cin >> num;
    StoreMess* list = new StoreMess[num];
    int count = 0;
    for (int i = 0; i < num; i++) {
        cout << "Enter the name: ";
        getline(cin, list[i].name);
        cout << "Enter the money: ";
        cin >> list[i].money;
        if (list[i].money > 10000) count++;
    }
    cout << "Grand Patrons" << endl;
    if (count > 0 ) {
        for (int i = 0; i < num; i++) {
            if (list[i].money > 10000) {
                cout << list[i].name << "\t" << list[i].money << endl;
            }
        }
    } else {
        cout << "none" << endl;
    }
    cout << "Patrons" << endl;
    if (count == num) {
        cout << "none" << endl;
    } else {
        for (int i = 0; i < num; i++) {
            if (list[i].money <= 10000) {
                cout << list[i].name << "\t" << list[i].money << endl;
            }
        }
    }
    delete[] list;
}

void handle_7() {
    cout << "Enter  words (q to quit):" << endl;
    int type1 = 0; // 元音开头
    int type2 = 0; // 辅音开头
    int type3 = 0; // 非字母开头
    string word;
    cin >> word;
    while (word != "q") {
        if (!isalpha(word[0])) type3++;
        else if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u') {
            type1++;
        } else {
            type2++;
        }
        cin >> word;
    }
    cout << type1 << " words beginning with vowels" << endl;
    cout << type2 << " words beginning with consonants" << endl;
    cout << type3 << " others" << endl;
}

void handle_8() {
    string fileName;
    cout << "Enter the file name: ";
    getline(cin, fileName);
    ifstream ifile;
    ifile.open(fileName);
    if (!ifile.is_open()) {
        cout << "Open fail" << endl;
        exit(EXIT_FAILURE);
    }
    int count = 0;
    char str;
    ifile >> str;
    while (ifile.good()) {
        count++;
        ifile >> str;
    }
    if (ifile.eof()) {
        cout << "This file has " << count << "characters" << endl;
    }
    ifile.close();
}

void handle_9() {
    string fileName;
    cout << "Enter the file name: ";
    getline(cin, fileName);
    ifstream ifile;
    ifile.open(fileName);
    if (!ifile.is_open()) {
        cout << "Open fail" << endl;
        exit(EXIT_FAILURE);
    }
    int num;
    ifile >> num;
    cout << num << endl;
    char str[30];
    ifile.getline(str, 1);
    StoreMess* list = new StoreMess[num];
    int count = 0;
    for (int i = 0; i < num; i++) {
        getline(ifile, list[i].name);
        cout << list[i].name << endl;
        ifile >> list[i].money;
        ifile.getline(str, 1);
        if (list[i].money > 10000) count++;
    }
    cout << "Grand Patrons" << endl;
    if (count > 0 ) {
        for (int i = 0; i < num; i++) {
            if (list[i].money > 10000) {
                cout << list[i].name << "\t" << list[i].money << endl;
            }
        }
    } else {
        cout << "none" << endl;
    }
    cout << "Patrons" << endl;
    if (count == num) {
        cout << "none" << endl;
    } else {
        for (int i = 0; i < num; i++) {
            if (list[i].money <= 10000) {
                cout << list[i].name << "\t" << list[i].money << endl;
            }
        }
    }
    delete[] list;
    ifile.close();
}

int main() {
    handle_9();
}

